<?php
class goods_api extends common_api {
	
	//获取商品列表
    public function get_goods($data){       
        $real = intval($data['real']);
		$uid = intval($data['uid']);
       // $limit = $this->get_page($data);         
        //生成查询条件
        $now = time();
        $condition = " goods_status='1' and goods_real='$real' ";
		//判断用户是否已经体验过2元话费充值
		if($uid){
			$result = $this->db->getOne("select order_id from data_order where order_goods_id = '2' and order_uid ='$uid'");
			if($result){
				$condition .= " and goods_id !='2'";
			}  
		}       

        //取数据
        $fields = " goods_id,goods_title,goods_score,(goods_buyer_count+goods_virtual_buyer_count) as goods_buyer_count,goods_photo,goods_description,goods_real";
		//$list = $this->db->getAll("SELECT ".$fields." FROM data_goods WHERE ".$condition." order by goods_create_time ASC ".$limit);
		$list = $this->db->getAll("SELECT ".$fields." FROM data_goods WHERE ".$condition." order by goods_id ASC ");
        //整理数据
        if ($list){
            $output['status'] = 1;
            foreach ($list as $k=>$v){
                $output['list'][$k]['goods_id'] = $v['goods_id'];
                $output['list'][$k]['goods_title'] = $v['goods_title'];
                $output['list'][$k]['goods_market_price'] = $v['goods_market_price'];
                $output['list'][$k]['goods_buyer_count'] = $v['goods_buyer_count']+$v['goods_virtual_buy_type'];		//实际购买人数+虚拟购买人数
                $output['list'][$k]['goods_score'] = $v['goods_score'];
				$output['list'][$k]['goods_description'] = $v['goods_description'];                  
                //取缩略图
                $photo_arr = explode('|',$v['goods_photo']);
                //$output['list'][$k]['goods_photo'] = SITE_ADDR . get_thumb_name($photo_arr[0], 'small');
				$output['list'][$k]['goods_photo'] = SITE_ADDR . $photo_arr[0];
				if($v['goods_real'] == '1'){
					$output['list'][$k]['goods_photo2'] = SITE_ADDR . $photo_arr[1];
				}
				
            }
        }else{
			$output['status'] = 0;
            $output['tips'] = '无商品信息!';
        }
        return $output;
    }
	
	
	// 商品兑换首页轮播图接口
    public function get_topshow($data){
        $output['status'] = 0;
        $limit = $this->get_page($data);
        //取数据
        $list = $this->db->getAll("SELECT * FROM data_top_show WHERE show_status='1' ".$limit);
        //整理数据
        if ($list){
            $output['status'] = 1;
            foreach ($list as $k=>$v){
                $output['list'][$k]['goods_id'] = $v['show_pid'];
                $output['list'][$k]['goods_title'] = $v['show_title'];
                $output['list'][$k]['goods_photo'] = SITE_ADDR.$v['show_photo'];
            }
        }else{
            $output['tips'] = '无轮播图信息!';
        }
        return $output;
    }
	
	 
	//商品详情
    public function get_goods_detail($data){
        $goods_id = intval($data['gid']);		
        
        //生成查询条件
        $now = time();
        $condition = "goods_status='1' and goods_id='".$goods_id."' ";
		
        //取数据
        $fields = " goods_id,goods_title,goods_buyer_count,goods_virtual_buy_type,goods_score,goods_photo,goods_description ";
        $list = $this->db->getRow("SELECT ".$fields." FROM data_goods WHERE ".$condition);
       
        //整理数据
        if ($list){
            $output = $list;
            $output['status'] = 1;
            $output['goods_buyer_count'] = $output['goods_buyer_count']+$output['goods_virtual_buy_type'];		//实际购买人数+虚拟购买人数
            $output['goods_end_time'] = "无时间限制";		//结束时间
            $output['goods_description'] = strip_tags($output['goods_description']);
            //取缩略图
            $photo_arr = explode('|',$list['goods_photo']);
            $photo_str = '';
            foreach($photo_arr as $k=>$v){
                $thumb_photo = SITE_ADDR . get_thumb_name($v, 'thumb');  
				$photo_arr[$k] = $thumb_photo;        
                //$photo_str .= $photo_str?'|'.$thumb_photo:$thumb_photo;
            }
            $output['goods_photo'] = $photo_arr;
        }else{
			$output['status'] = 0;
            $output['tips'] = '无商品信息!';
        }
        return $output;
    }
	
	 //商品兑换接口
    function add_goods_order($data) {
        $uid = intval($data['exchange_uid']);
        $gid = intval($data['exchange_gid']);       
        $consignee_name = trim($data['exchange_name']);
        $consignee_phone = trim($data['exchange_phone']);
        $consignee_address = trim($data['exchange_address']);
        $remark = $data['exchange_comment'];        
        $pwd = base64_encode(md5($data['pwd']));       
        $output['status'] = 0;
        //取得用户和商品信息
        $user_info = $this->db->getRow("SELECT * FROM data_user WHERE user_id='".$uid."'");		
        //密码
        if($user_info['user_pwd'] !== $pwd){
            $output['tips'] = '密码不正确!';
            return $output;
        }
        //账户是否激活
        if($user_info['user_check'] == '0'){
            $output['tips'] = '请先验证账户!';
            return $output;
        }
        //商品信息
        $goods_info = $this->db->getRow("SELECT * FROM data_goods WHERE goods_id='".$gid."' and goods_status='1' ");
        if (!$goods_info){
            $output['tips'] = '无该兑换信息!';
            return $output;
        }
    	
		//判断用户是否已经体验过2元话费的充值
		if($gid == '2' && $user_info['user_is_experience'] == '1'){
			$output['tips'] = '订单提交失败,您已经体验过2元话费充值,请选择其他的充值!';
            return $output;		
		}

        //购买操作
        $goods_score = $goods_info['goods_score'];     //兑换所需要的积分
        if ($user_info['user_score'] >= $goods_score){
            $this->db->begin();		//启动事务
            //用户扣费
            $r=$this->db->update('data_user',array('user_score'=>'user_score-'.$goods_score),"user_id='".$uid."'");
            //订单数据入库
            $order_sn = date("ymd")."-".$gid."-".random_code(4,'NUMBER');
            $insert_order['order_uid'] = $uid;
            $insert_order['order_user'] = $user_info['user_name'];
            $insert_order['order_goods_id'] = $gid;
            $insert_order['order_goods_title'] = $goods_info['goods_title'];
            $insert_order['order_market_price'] = $goods_info['goods_market_price'];
            $insert_order['order_price'] = $goods_info['goods_price'];          
            $insert_order['order_sn'] = $order_sn;
            $insert_order['order_score'] = $goods_info['goods_score'];          
            $insert_order['order_express_address'] = $consignee_address;
            $insert_order['order_user_phone'] = $consignee_phone;
            $insert_order['order_consignee_name'] = $consignee_name;
            $insert_order['order_remark'] = $remark;
            $insert_order['order_create_time'] = date('Y-m-d H:i:s');
            $insert_order['order_update_time'] = date('Y-m-d H:i:s');            
            $insert_order['order_user_qq'] = $qq;            
            $insert_order['order_user_zip_code'] = $zip;
			$insert_order['order_isreal'] = $goods_info['goods_real'];
            $r1=$this->db->insert('data_order',$insert_order);
            //记录掌币日志
            load_file('log_class',LIB_PATH);
            $log = new Log($this->db);
            $count = "-".$goods_score;
            $content = "兑换商品(".$goods_info['goods_title'].")";			
            $r2 = $log->log($count, $uid, $user_info['user_name'], 'user_goods_exchange', $content);			
            //更新商品的购买人数
            $r3 = $this->db->update('data_goods',array('goods_buyer_count'=>'goods_buyer_count+1'),"goods_id='".$gid."'");
			
            //执行事务
            if ($r && $r1 && $r2 && $r3){
                $this->db->commit();
                $output['status'] = 1;
                $output['tips'] = '订单提交成功!';
            }else {
                $this->db->back();				
                $output['tips'] = '订单提交失败!';
            }
            return $output;
        }else {			
            $output['tips'] = '您的积分不足!';
            return $output;
        }

    }
	
	
	 //订单记录列表
    function get_order_log($data) {
        $uid = intval( $data['uid'] );
        $limit = $this->get_page($data);        
        //取记录数据
        $fields = "order_goods_title,order_score,order_status, DATE_FORMAT(order_create_time,'%Y-%m-%d') AS order_create_time";     
        $list = $this->db->getAll("SELECT ".$fields." FROM data_order WHERE order_uid='".$uid."' ORDER BY order_id DESC ".$limit);
        if ($list) {          
			$output['status'] = 1;    
			$output['list'] = $list;
        }else{
			$output['status'] = 0;
            $output['tips'] = '没有订单记录!';
        }
        return $output;
    }

    //订单记录详情
    function get_order_detail($data) {
        $order_id = intval( $data['order_id'] );
        //取记录数据
        $fields = " order_id,order_uid,order_goods_id,order_goods_title,order_express_sn,order_num,order_create_time,order_confirm_time,order_coin_total,order_coin,order_deliver_time,order_user_phone,order_remark,order_consignee_name,order_status,order_express_name,order_express_address,goods_photo,goods_type2,order_user_qq,order_market_price";
        //$this->db->debug(1,1);
        $list = $this->db->getRow("SELECT ".$fields." FROM data_order,data_goods WHERE order_id='".$order_id."' and order_goods_id=goods_id");
        if ($list) {
            $output = $list;
            $output['status'] = 1;
            $photo_arr = explode('|',$list['goods_photo']);
            $output['goods_small_photo'] = SITE_ADDR . get_thumb_name($photo_arr[0], 'small');
            $output['goods_thumb_photo'] = SITE_ADDR . get_thumb_name($photo_arr[0], 'thumb');
        }else{
            $output['tips'] = '没有该订单记录!';
        }
        return $output;
    }
	
	// 订单确认
    function confirm_goods_order($data) {
        $uid = intval($data['uid']);
        $oid = intval($data['oid']);
        $pwd = trim($data['pwd']);
        $output['status'] = 0;
        //用户信息
        $sql = "SELECT user_id,user_name,user_chance_count,user_check,user_status FROM data_user where user_id='" . $uid . "' AND user_pwd='" . base64_encode(md5($pwd)) . "'";
        $user_info = $this->db->getRow($sql);
        if($user_info){
            //订单信息
            $sql1 = "SELECT order_id FROM data_order where order_id='" . $oid . "' and order_uid='".$uid."' and order_status='5'";            
            $order_info = $this->db->getRow($sql1);
            if($order_info){
                $update_order = array('order_status'=>2,'order_confirm_time'=>date('Y-m-d H:i:s'),'order_update_time'=>date('Y-m-d H:i:s'));
                $res = $this->db->update('data_order',$update_order,"order_id='".$oid."'");
                if($res){
                    $output['status'] = 1;
                    $output['tips'] = '确认收货成功!';
                }else{
                    $output['tips'] = '确认收货失败!';
                }
            }else{
                $output['tips'] = '无该订单信息!';
            }
        }else{
            $output['tips'] = '密码错误!';
        }
        return $output;
    }
	
   
}

?>
